package org.shj.book2.chapter09.improve;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Host {
	private ExecutorService executor = Executors.newCachedThreadPool();

	public Future<String> request(final int count, final char c){
		Future<String> future = executor.submit(new Callable<String>(){
			public String call() throws Exception {
				System.out.println("        making RealData(" + count + ", " + c + ") begin");
				char[] buffer = new char[count];
				for(int i = 0 ; i < count ; i++){
					buffer[i] = c;
					try{
						Thread.sleep(100);
					}catch(InterruptedException e){
						
					}
				}
				System.out.println("        making RealData(" + count + ", " + c + ") end");
				return new String(buffer);
			}			
		});
		
		return future;
	}
	
	public void shutdown(){
		executor.shutdown();
	}
}
